package com.lanou.horo.zhuizhui.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.lanou.horo.zhuizhui.entity.SearchIconItem;
import com.lanou.horo.zhuizhui.utility.imageLoader.AsyncImageLoader;
import com.lanou.horo.zhuizhui.utility.clickInterface.HomePageItemClickListener;
import com.lanou.horo.zhuizhui.utility.clickInterface.ItemLongClickListener;
import com.lanou.horo.zhuizhui.utility.customImage.RoundAngleImageView;
import com.lanou.horo.zhuizhui.R;
import com.lanou.horo.zhuizhui.utility.imageLoader.VolleySingleton;

import java.util.List;

/**
 * Created by dllo on 15/10/31.
 */
public class SearchIconAdapter extends RecyclerView.Adapter<SearchIconAdapter.SearchIconViewHolder> {
    private List<SearchIconItem.ThemesEntity> entities;
    private ItemLongClickListener longClickListener;
    private HomePageItemClickListener itemClickListener;
    private AsyncImageLoader asyncImageLoader;
    private Context context;
    private ImageLoader imageLoader;

    public List<SearchIconItem.ThemesEntity> getEntities() {
        return entities;
    }

    public void setItemClickListener(HomePageItemClickListener itemClick) {
        this.itemClickListener = itemClick;
    }

    public void setLongClickListener(ItemLongClickListener listener){
        this.longClickListener = listener;
    }

    public SearchIconAdapter(Context context) {
        this.context = context;
        imageLoader = VolleySingleton.getInstance().getImageLoader();
        asyncImageLoader = new AsyncImageLoader(context);
    }

    public void addSeaList(List<SearchIconItem.ThemesEntity> datas) {
        entities = datas;
        notifyDataSetChanged();
    }

    @Override
    public SearchIconViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.fragment_page_search_item, null);
        return new SearchIconViewHolder(view);
    }

    @Override
    public void onBindViewHolder(SearchIconViewHolder holder, int position) {
        if (entities != null && entities.size() > 0) {
            SearchIconItem.ThemesEntity entity = entities.get(position);
            holder.searchTextView.setText(entity.getName());
            String url = entity.getCover();
            holder.searchImg.setImageUrl(url, imageLoader);
 //           setBitmap(url, holder.searchImg);
            holder.itemPosition = position;
        }

    }

    @Override
    public int getItemCount() {
        return entities != null && entities.size() > 0 ? entities.size() : 0;
    }


    public class SearchIconViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
        private TextView searchTextView;
        private RoundAngleImageView searchImg;
        private int itemPosition;

        public SearchIconViewHolder(View itemView) {
            super(itemView);
            searchTextView = (TextView) itemView.findViewById(R.id.searchTextView);
            searchImg = (RoundAngleImageView) itemView.findViewById(R.id.searchImg);
            itemView.setOnClickListener(this);
            itemView.setOnLongClickListener(this);
            searchImg.setImageResource(R.mipmap.loading);
        }

        @Override
        public void onClick(View v) {
            if (entities != null) {
                SearchIconItem.ThemesEntity entity = entities.get(itemPosition);
                if (itemClickListener != null) {
                    itemClickListener.searcherItemClick(entity);
                }
            }
        }

        @Override
        public boolean onLongClick(View v) {
            if (entities != null) {
                SearchIconItem.ThemesEntity entity = entities.get(itemPosition);
                if (longClickListener != null) {
                    longClickListener.searcherItemLongClick(entity);
                }
            }
            return true;
        }
    }

    private void setBitmap(String url, ImageView imageView) {
        // 给 ImageView 设置一个 tag
        imageView.setTag(url);
        // 预设一个图片
        imageView.setImageResource(R.mipmap.loading);
        if (!TextUtils.isEmpty(url)) {
            Bitmap bitmap = asyncImageLoader.loadImage(imageView, url);
            if (bitmap != null) {
                imageView.setImageBitmap(bitmap);
            }
        }
    }
}
